#ifndef _DSWCONSOLE_RF_915_H
#define _DSWCONSOLE_RF_915_H

#include "dswconsole_base.h"
#include "rf_def.h"

#define RF_915_COMM_HEAD "\xc0\x53\x06"
#define RF_915_COMM_HEAD_SIZE 3

#define RF_915_RECV_TIMEOFFSET 1

typedef std::list<LPST_RFSend_915> LIST_SEND_915_T, *LPLIST_SEND_915_T;

class CDswconsole_rf_915 : public CDswconsole_base
{
public:
	CDswconsole_rf_915(LPST_ENV_T pEnv, EN_CON_SESSION_TYPE nType, LPST_CONSOLE_INFO_T pInfo);
	virtual ~CDswconsole_rf_915();
public:
	static CDswconsole_rf_915* createNew(LPST_ENV_T pEnv);
public:
	void ResetRollCode(bool bPL, lpst_sl_t pSl);
	void BroadcastConsoleCmd(void* pParam);

	static int cbOnHanderRemote(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderRemote(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderEmerg(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderEmerg(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderDoor(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderDoor(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderSmoke(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderSmoke(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHandervirberion(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHandervirberion(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderCurtain(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderCurtain(pRecv_tlv, ncmd_rsp, pResult_tlv);};

	static int cbOnHanderBWR(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderBWR(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderCO(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderCO(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderFZ(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv, void * pCaller){return ((CDswconsole_rf_915*)pCaller)->OnHanderFZ(pRecv_tlv, ncmd_rsp, pResult_tlv);};

	static void cbBroadcastSetGarrison(int nCmd, void* pParam, void* pCaller){((CDswconsole_rf_915*)pCaller)->BroadcastSetGarrison(nCmd, pParam);};
	static void cbBroadcastSetAreAlarm(int nCmd, void* pParam, void* pCaller){((CDswconsole_rf_915*)pCaller)->BroadcastSetAreAlarm(nCmd, pParam);};
private:
	virtual bool SeekToStartComm(LPST_COMM_RECV_T pCommRecv, int &nPayloadSize);
	virtual void ProcessData(char* pData, int nLen);
private:
	static void cbHeartBeet(int fd, short events, void* ctx){((CDswconsole_rf_915*)ctx)->HeartBeet(fd, events);};
	void HeartBeet(int fd, short events);
	void SendData(LPST_RFRecv_915 pTlv){evbuffer_add(bufferevent_get_output(m_pBev), pTlv, 3 + pTlv->nlen);};
	void Send915Data(LPST_RFSend_915 pTlv);

	int OnHanderRemote(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	void DealHandOn(LPST_RFRecv_915 pTlv);
	void DealHandOff(LPST_RFRecv_915 pTlv);
	void DealHandMut(LPST_RFRecv_915 pTlv);
	void DealHandSOS(LPST_RFRecv_915 pTlv);
	
	int OnHanderEmerg(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	int OnHanderDoor(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	int OnHanderSmoke(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	int OnHandervirberion(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	int OnHanderCurtain(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	
	int OnHanderBWR(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	int OnHanderCO(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);
	int OnHanderFZ(LPST_RFRecv_915 pRecv_tlv, int ncmd_rsp, LPST_RFRecv_915 pResult_tlv);

	void BroadcastSetGarrison(int nCmd, void* pParam);
	void BroadcastSetAreAlarm(int nCmd, void* pParam);

	static void cbSendTask(int fd, short events, void* ctx){((CDswconsole_rf_915*)ctx)->SendTask(fd, events);};
	void SendTask(int fd, short events);
	
	void SetSoundLight(bool bOn);
	void SetTrigSoundLight();
	void SetPoliceLock(bool bOn);

	bool IsInSavedRFID(LPST_RFRecv_915 pTlv, LPST_ARE_PARAM pSaved);

	int LearnRFID(LPST_RFRecv_915 pTlv, LPST_ARE_PARAM pSaved);
private:
	timer_param_t m_nTimer, m_nSendTask;
	unsigned int m_nLastRfid;
	time_t m_nLastRecvTime;
	LIST_SEND_915_T m_nSendList;
	pthread_mutex_t m_nLock;
};

#endif

